package com.cm.figure.service.userService;

import com.cm.figure.dao.UserMapper;
import com.cm.figure.model.Server;
import com.cm.figure.model.User;
import com.cm.figure.utility.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ClassName:RegisterUserServiceImp
 * Package:com.cm.figure.service.userService
 * Description:
 *
 * @Date:2021/8/14 16:40
 * @com.chuangmei
 */
@Service
public class RegisterUserServiceImp implements RegisterUserService {

    // dao
    @Resource
    private UserMapper userDao;

    /**
     * 注册用户接口实现
     * @param user
     * @return User
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public User registerUser(User user) {

        /**生成每个用户的上传路径*/
        String filepath = "";
        if (Server.isLocal){
            filepath = Server.uploadPathPrefixLocal + user.getUsername();
        }else{
            // 服务器路径
            filepath = Server.uploadPathPrefixServer + user.getUsername();
        }

        user.setAvatar("http://www.blogry.cn/images/avatar/default.jpg");
        user.setNickname("昵称");
        user.setCreattime(new SimpleDateFormat().format(new Date()));
        user.setUpdatetime(new SimpleDateFormat().format(new Date()));
        user.setFilepath(filepath);
        // MD5加密
        user.setPassword(MD5.getMD5(user.getPassword()));

        // 初始化返回参数
        User responseUser = null;

        // 获取用户
        if (userDao.insertSelective(user) > 0) {
            responseUser = userDao.selectByPrimaryKey(user.getId());
        }

        return responseUser;
    }
}
